
import dzmodel, dzentity, dzutil

class Subject(dzentity.Entity):
    def __init__(self,rec=None,table=None):
        dzentity.Entity.__init__(self,rec)
        if not rec:
            self.NAME      = ''
            self.PRIORITY  = 5
            self.DESCR     = ''

    def valid(self):
        def test(condition,message):
            if not condition:
                messages.append(message)
        messages = []
        test(len(self.NAME) > 1,"Name too short")

        for m in messages: dzutil.warning(m)
        return len(messages)==0

    def update(self,keywords):
        for key in self.keys():
            if key in keywords:
                self.__dict__[key] = keywords[key]

class Subjects(dzmodel.Table):
    def __init__(self):
        dzmodel.table_prefix = 'dz_'
        dzmodel.Table.__init__(self,Subject)
        self.table_key = 'SUBJECTID'


if __name__ == '__main__':

    import unittest

    class test_Subject(unittest.TestCase):

        def test_create(self):
            subject = Subject()
            assert subject

        def test_valid(self):
            subject = Subject()
            assert not subject.valid()
            subject.NAME = 'longer name'
            assert subject.valid()

    class test_Subjects(unittest.TestCase):

        def test_Create(self):
            subjects = Subjects()
            assert subjects <> None

        def test_insert_delete(self):
            subjects = Subjects()
            subject = subjects.new()
            subject.NAME = 'test'
            newid = subjects.insert(subject)
            assert newid > 0

            subject = subjects.seek(newid)
            assert subject

            subjects.delete(subject)
            subject = subjects.seek(newid)
            assert not subject

    unittest.main()
